package se.rkrantz.simpleraid.hibernate.domain;

import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;


@Entity
@Table(name = "USER")
public class User {
	private String username;
	private String password;
	private String email;
	private DateTime signupDate;
	private List<String> roles;
	
	private Set<WowCharacter> characters;
	private WowCharacter primaryCharacter;
	
	@Id
	@Column(name = "USERNAME", nullable = false, unique = true)
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	@Column(name = "PASSWORD", nullable = false)
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Column(name = "EMAIL", nullable = false)
	public String getEmail() {
		return email;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
	@Column(name = "SIGNUP_DATE", nullable = false)
	@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
	public DateTime getSignupDate() {
		return signupDate;
	}
	
	public void setSignupDate(DateTime signupDate) {
		this.signupDate = signupDate;
	}
	
	@Column(name = "ROLES", nullable = false)
	@ElementCollection(fetch = FetchType.EAGER)
	public List<String> getRoles() {
		return roles;
	}
	
	public void setRoles(List<String> roles) {
		this.roles = roles;
	}
	
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinTable(name = "CHARACTER_USER", joinColumns = { @JoinColumn(name = "USERNAME")}, inverseJoinColumns = { @JoinColumn(name = "CHARACTER_ID")})
	@Sort(type=SortType.NATURAL)
	public Set<WowCharacter> getCharacters() {
		return characters;
	}
	
	public void setCharacters(Set<WowCharacter> characters) {
		this.characters = characters;
	}
	
	@OneToOne(cascade = CascadeType.ALL)
	public WowCharacter getPrimaryCharacter() {
		return primaryCharacter;
	}
	
	public void setPrimaryCharacter(WowCharacter primaryCharacter) {
		this.primaryCharacter = primaryCharacter;
	}
}
